/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.internal.platform;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.exceptions.LyciaInitializationException;

public class PlatformFactoryProvider {

	private static final String ANDROID1_6PLATFORM_FACTORY_CLASS = "org.sourceprojects.lycia.internal.platform.android.Android1_6PlatformFactoryImpl";
	private static final String ANDROID2_2PLATFORM_FACTORY_CLASS = "org.sourceprojects.lycia.internal.platform.android.Android2_2PlatformFactoryImpl";
	private static final String BASIC_PLATFORM_FACTORY_CLASS = "org.sourceprojects.lycia.internal.platform.base.BasicPlatformFactoryImpl";

	private PlatformFactoryProvider() {
	}

	@SuppressWarnings("unchecked")
	public static final <B> PlatformFactory<B> newInstance(
			final LyciaParser<B> lyciaParser) {

		Class<? extends PlatformFactory<B>> factoryClass;

		try {
			if (PlatformSupportUtils.isDalvikDependingPlatform()) {
				if (PlatformSupportUtils.getAndroidApiVersion().getApiLevel() < 8) {
					factoryClass = (Class<? extends PlatformFactory<B>>) Class
							.forName(ANDROID1_6PLATFORM_FACTORY_CLASS);
				} else {
					factoryClass = (Class<? extends PlatformFactory<B>>) Class
							.forName(ANDROID2_2PLATFORM_FACTORY_CLASS);
				}
			} else {
				factoryClass = (Class<? extends PlatformFactory<B>>) Class
						.forName(BASIC_PLATFORM_FACTORY_CLASS);
			}

			final Constructor<? extends PlatformFactory<B>> constructor = factoryClass
					.getConstructor(LyciaParser.class);

			return constructor.newInstance(lyciaParser);

		} catch (final ClassNotFoundException e) {
			throw new LyciaInitializationException(e);
		} catch (final SecurityException e) {
			throw new LyciaInitializationException(e);
		} catch (final NoSuchMethodException e) {
			throw new LyciaInitializationException(e);
		} catch (final IllegalArgumentException e) {
			throw new LyciaInitializationException(e);
		} catch (final InstantiationException e) {
			throw new LyciaInitializationException(e);
		} catch (final IllegalAccessException e) {
			throw new LyciaInitializationException(e);
		} catch (final InvocationTargetException e) {
			throw new LyciaInitializationException(e);
		}
	}

}
